import json
import concurrent.futures
from Agent.Planer import Planer

def process_single_question(data_line):
    """处理单个问题的函数"""
    try:
        data = json.loads(data_line)
        planer = Planer(data['question'])
        data['plan'] = planer.messages[-1]['content']
        return data
    except Exception as e:
        print(f"处理问题时出错: {e}")
        return None

def main():
    # 读取所有问题
    questions = []
    with open('./data/question_c.json', encoding='utf8') as f:
        for line in f:
            questions.append(line.strip())
    
    # 使用50个线程的线程池并行处理
    results = []
    with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
        # 提交所有任务
        future_to_question = {executor.submit(process_single_question, q): q for q in questions}
        
        # 收集结果
        for future in concurrent.futures.as_completed(future_to_question):
            result = future.result()
            if result:
                results.append(result)
    
    # 将结果写入文件
    with open('./data/plan.jsonl', 'w', encoding='utf8') as fw:
        for result in results:
            fw.write(json.dumps(result, ensure_ascii=False) + '\n')

if __name__ == "__main__":
    main()